package com.qzsoft.system.modules.permission.deptJob.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.service.BaseService;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.jeemis.common.utils.ExcelUtils;
import com.qzsoft.system.modules.permission.deptJob.dao.SysUserDeptMapper;
import com.qzsoft.system.modules.permission.user.dto.UserDept;
import com.qzsoft.system.modules.permission.user.dto.UserJob;
import com.qzsoft.system.repository.sys.entity.SysUserDeptEntity;
import com.qzsoft.system.modules.permission.deptJob.service.SysUserDeptService;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * (SysUserDept)表服务实现类
 *
 * @author yzz
 * @since 2020-07-30 09:43:11
 */
@Service("sysUserDeptService")
@Log4j2
public class SysUserDeptServiceImpl extends BaseService implements SysUserDeptService {
    private final SqlSession sqlSession;
    @Autowired
    private SysUserDeptMapper mapper;

    public SysUserDeptServiceImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    /**
     * TbNews数据分页
     * @param params
     * @return IPage
     */
    @Override
    public IPage<SysUserDeptEntity> page(Map<String, Object> params) {
        IPage<SysUserDeptEntity> page =this.getPage(params, Constant.CREATE_DATE,false,SysUserDeptEntity.class) ;
        QueryWrapper<SysUserDeptEntity> queryWrapper =this.getQueryWrapper(params,SysUserDeptEntity.class);
        IPage<SysUserDeptEntity> pageData = mapper.selectPage(page, queryWrapper);

        pageData= ConvertDictUtils.formatDicDataPage(pageData);
        return pageData;
    }
    /**
     * TbNews数据列表
     * @param params
     * @return
     */
    @Override
    public List<SysUserDeptEntity> list(Map<String, Object> params) {
        QueryWrapper<SysUserDeptEntity> queryWrapper = this.getQueryWrapper(params,SysUserDeptEntity.class);
        List<SysUserDeptEntity> entityList = mapper.selectList(queryWrapper);
        entityList=ConvertDictUtils.formatDicDataList(entityList);
        return entityList;
    }

    @Override
    public SysUserDeptEntity get(String id) {
        SysUserDeptEntity entity = mapper.selectById(id);
        return entity;
    }

    @Override
    public void save(SysUserDeptEntity entity) {
        mapper.insert(entity);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUserDeptEntity entity) {
        mapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        mapper.deleteBatchIds(Arrays.asList(ids));
    }
    @Override
    public void exportXls(Map<String, Object> params , HttpServletResponse response)  {
        List<SysUserDeptEntity> list = this.list(params);
        ExcelUtils.exportExcelToTarget(response, "机构职位导出列表", list, SysUserDeptEntity.class);
    }

    @Override
    public UserDept getMainDeptByUsername(String username) {
        return mapper.getMainDeptByUsername(username);
    }

    @Override
    public List<UserDept> getSubDeptByUsername(String username) {
        return mapper.getSubDeptByUsername(username);
    }

    @Override
    public UserJob getMainJobByUsername(String username) {
        return mapper.getMainJobByUsername(username);
    }

    @Override
    public List<UserJob> getSubJobByUsername(String username) {
        return mapper.getSubJobByUsername(username);
    }
}